package com.doudou.collection.adapter.controller;

import com.doudou.collection.domain.service.UserService;
import com.doudou.collection.infrastructure.common.security.CurrentPrincipal;
import com.doudou.collection.infrastructure.common.web.ResultVO;
import com.doudou.collection.orm.dto.user.login.*;
import com.doudou.collection.orm.dto.user.reg.*;
import com.doudou.collection.orm.dto.user.update.*;
import com.doudou.collection.orm.vo.UserLoginSimpleVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName AuthenticationController<br>
 * @Description 用户认证管理模块<br>
 * @Author 李浩文<br>
 * @Date 2023/5/31 20:05<br>
 * @Version 1.0<br>
 */
@Api(tags = "01.认证管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/users")
public class AuthenticationController {

    @Autowired
    private UserService userService;

    @PostMapping("/reg/username")
    @ApiOperationSupport(order = 10)
    @ApiOperation("用户名注册功能")
    public ResultVO reg(@Validated @RequestBody UserRegDTO userRegDTO) {
        log.debug("正在处理用户名密码登录请求 ==> {}", userRegDTO);
        userService.addNewByUsername(userRegDTO);
        return ResultVO.ok();
    }

    @PostMapping("/reg/phone/pwd")
    @ApiOperation("手机号密码注册功能")
    @ApiOperationSupport(order = 11)
    public ResultVO regByPhoneAndPassword(@Validated @RequestBody PhoneRegDTO phoneRegDTO) {
        log.debug("正在处理手机号密码登录请求 ==> {}", phoneRegDTO);
        userService.addNewByPhone(phoneRegDTO);
        return ResultVO.ok();
    }

    @PostMapping("/reg/phone/code")
    @ApiOperation("手机号验证码注册功能")
    @ApiOperationSupport(order = 12)
    public ResultVO regByPhoneAndCode(@Validated @RequestBody PhoneCodeRegDTO phoneCodeRegDTO) {
        log.debug("正在处理手机号验证码登录请求 ==> {}", phoneCodeRegDTO);
        userService.addNewByPhoneAndCode(phoneCodeRegDTO);
        return ResultVO.ok();
    }

    @PostMapping("/reg/email/pwd")
    @ApiOperation("邮箱密码注册功能")
    @ApiOperationSupport(order = 13)
    public ResultVO regByEmailAndPassword(@Validated @RequestBody EmailRegDTO emailRegDTO) {
        log.debug("正在处理邮箱密码登录请求 ==> {}", emailRegDTO);
        userService.addNewByEmail(emailRegDTO);
        return ResultVO.ok();
    }

    @PostMapping("/reg/email/code")
    @ApiOperation("邮箱验证码注册功能")
    @ApiOperationSupport(order = 14)
    public ResultVO regByEmailAndCode(@Validated @RequestBody EmailCodeRegDTO emailCodeRegDTO) {
        log.debug("正在处理用户名密码登录请求 ==> {}", emailCodeRegDTO);
        userService.addNewByEmailAndCode(emailCodeRegDTO);
        return ResultVO.ok();
    }








    @PostMapping("/login/username")
    @ApiOperation("用户名密码登录功能")
    @ApiOperationSupport(order = 20)
    public ResultVO login(@Validated @RequestBody UserLoginDTO userLoginDTO) {
        log.debug("正在处理用户名密码登录请求 ==> {}", userLoginDTO);
        UserLoginSimpleVO user = userService.loginByUsername(userLoginDTO);
        return ResultVO.ok(user);
    }

    @PostMapping("/login/email/pwd")
    @ApiOperation("邮箱密码登录功能")
    @ApiOperationSupport(order = 21)
    public ResultVO loginByEmailAndPassword(@Validated @RequestBody EmailLoginDTO emailLoginDTO) {
        log.debug("正在处理邮箱密码登录请求 ==> {}", emailLoginDTO);
        UserLoginSimpleVO user = userService.loginByEmail(emailLoginDTO);
        return ResultVO.ok(user);
    }

    @PostMapping("/login/email/code")
    @ApiOperation("邮箱验证码登录功能")
    @ApiOperationSupport(order = 22)
    public ResultVO loginByEmailAndCode(@Validated @RequestBody EmailCodeLoginDTO emailCodeLoginDTO) {
        log.debug("正在处理邮箱验证码登录请求 ==> {}", emailCodeLoginDTO);
        UserLoginSimpleVO user = userService.loginByEmailAndCode(emailCodeLoginDTO);
        return ResultVO.ok(user);
    }

    @PostMapping("/login/phone/pwd")
    @ApiOperation("手机号密码登录功能")
    @ApiOperationSupport(order = 23)
    public ResultVO loginByPhoneAndPassword(@Validated @RequestBody PhoneLoginDTO phoneLoginDTO) {
        log.debug("正在处理手机号密码登录请求 ==> {}", phoneLoginDTO);
        UserLoginSimpleVO user = userService.loginByPhone(phoneLoginDTO);
        return ResultVO.ok(user);
    }

    @PostMapping("/login/phone/code")
    @ApiOperation("手机号验证码登录功能")
    @ApiOperationSupport(order = 24)
    public ResultVO loginByPhoneAndCode(@Validated @RequestBody PhoneCodeLoginDTO phoneCodeLoginDTO) {
        log.debug("正在处理手机号验证码登录请求 ==> {}", phoneCodeLoginDTO);
        UserLoginSimpleVO user = userService.loginByPhoneAndCode(phoneCodeLoginDTO);
        return ResultVO.ok(user);
    }






    @PostMapping("/logout")
    @ApiOperation("登出功能")
    @ApiOperationSupport(order = 30)
    public void logout() {
        log.debug("正在处理用户登出的请求");
        SecurityContextHolder.clearContext();
    }

    @PostMapping("/{userId}/update/detail")
    @ApiOperation("修改个人信息")
    @ApiOperationSupport(order = 40)
    public ResultVO update(@RequestBody UserDetailDTO userDetailDTO,@PathVariable Long userId) {
        log.debug("正在处理修改个人详细信息请求, {}", userDetailDTO);
        userDetailDTO.setUserId(userId);
        log.debug("!!!!!!!!{}",userDetailDTO.getUserId());
        log.debug("正在处理修改个人详细信息请求--------------------------------, {}", userId);
        userService.updateUserDetailById(userDetailDTO);
        return ResultVO.ok();
    }

    @PostMapping("/update/username")
    @ApiOperation("修改用户名")
    @ApiOperationSupport(order = 41)
    public ResultVO updateUsernameById(@Validated @RequestBody UserUpdateNameDTO userUpdateNameDTO) {
        log.debug("正在处理修改个人用户名的请求, {}", userUpdateNameDTO);
        userService.updateUsernameById(userUpdateNameDTO);
        return ResultVO.ok();
    }

    @PostMapping("/update/pwd")
    @ApiOperation("修改密码")
    @ApiOperationSupport(order = 42)
    public ResultVO updatePasswordById(@Validated @RequestBody UserUpdatePwdDTO userUpdatePwdDTO) {
        log.debug("正在处理修改个人密码的请求, {}", userUpdatePwdDTO);
        userService.updatePasswordById(userUpdatePwdDTO);
        return ResultVO.ok();
    }

    @PostMapping("/update/email")
    @ApiOperation("修改邮箱")
    @ApiOperationSupport(order = 43)
    public ResultVO updateEmailById(@Validated @RequestBody UserUpdateEmailDTO userUpdateEmailDTO) {
        log.debug("正在处理修改个人邮箱的请求, {}", userUpdateEmailDTO);
        userService.updateEmailById(userUpdateEmailDTO);
        return ResultVO.ok();
    }

    @PostMapping("/update/phone")
    @ApiOperation("修改手机号")
    @ApiOperationSupport(order = 44)
    public ResultVO updatePhoneById(@RequestBody UserUpdatePhoneDTO userUpdatePhoneDTO) {
        log.debug("正在处理修改个人手机号的请求, {}", userUpdatePhoneDTO);
        userService.updatePhoneById(userUpdatePhoneDTO);
        return ResultVO.ok();
    }

    @GetMapping("/code/phone/{phone}")
    @ApiOperation("发送手机验证码")
    @ApiOperationSupport(order = 50)
    public ResultVO sendCodeToPhone(@PathVariable String phone) {
        userService.sendCodeToPhone(phone);
        return ResultVO.ok();
    }

    @GetMapping("/code/email/{email}")
    @ApiOperation("发送邮箱验证码")
    @ApiOperationSupport(order = 51)
    public ResultVO sendCodeToEmail(@PathVariable String email) {
        userService.sendCodeToEmail(email);
        return ResultVO.ok();
    }
}



















